﻿using PermissionManager.Model;
using PermissionsManager.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PermissionManager.BLL
{
    public class MenuService:BaseService<Menu>
    {
        /// <summary>
        /// 新增得到功能和菜单
        /// </summary>
        /// <returns></returns>
        public List<MenuExtend> GetFunctionsByAdd()
        {
            using (var db = new PermissionManagerEntities())
            {
                var result = from a in db.Menu
                             join b in db.Function
                             on a.Id equals b.MenuId into c
                             from d in c.DefaultIfEmpty()
                             select new MenuExtend()
                             {
                                 Id = a.Id,
                                 FunId = d.Id,
                                 FunName = d.Name,
                                 Name = a.Name,
                                 ParentId=a.ParentId,
                                 SortId=a.SortId
                             };
                return result.ToList();
            }
        }

        /// <summary>
        /// 修改得到功能和菜单
        /// </summary>
        /// <returns></returns>
        public List<MenuExtend> GetFunctionsByUpdate(int RId)
        {
            using (var db = new PermissionManagerEntities())
            {
                var result = (from a in db.Menu                            
                             select new MenuExtend()
                             {
                                 Id = a.Id,                                         
                                 Name = a.Name,
                                 ParentId = a.ParentId,
                                 SortId = a.SortId,
                                 IsChecked=false
                             }).ToList();
                var fun = (from a in db.Function                          
                          select new MenuExtend()
                          {
                              Id = -a.Id,//防止Id冲突
                              Name = a.Name,
                              ParentId = a.MenuId,
                              //
                              IsChecked =(from b in db.RoleFunctionPermission
                                           where b.MenuId==a.MenuId&&b.RoleId==RId
                                           select b.FunctionId).Contains(a.Id)
                          }).ToList();
                return result.Concat(fun).ToList();
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool UpdateIsshow(Menu menus)
        {
            using (var db = new PermissionManagerEntities())
            {
                Menu menu = db.Menu.FirstOrDefault(a => a.Id == menus.Id);
                menu.IsShow = menus.IsShow;
                return db.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool UpdateMenu(Menu menu)
        {
            using (var db = new PermissionManagerEntities())
            {
                Menu menus = db.Menu.FirstOrDefault(a => a.Id == menu.Id);
                menus.Name = menu.Name;
                menus.ParentId = menu.ParentId;
                menus.Url = menu.Url;
                menus.IsShow = menu.IsShow;
                menus.SortId = menu.SortId;
                return db.SaveChanges() > 0;
            }
        }

     
    }
}
